{include file="public/header" /}


<div id="modal-edit" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content radius">
            <div class="modal-header">
                <h3 class="modal-title">菜单编辑</h3>
                <a class="close" data-dismiss="modal" aria-hidden="true" href="javascript:void();">×</a>
            </div>
            <div class="modal-body">
                <form class="form-horizontal"  id="add_menu" method="post" action="{:URL("menuadd")}">

                    <div class="form-group">
                        <label class="col-sm-3 control-label">上级分类：</label>
                        <div class="input-group col-sm-4">
                            <select id="pid" class="form-control m-b chosen-select" name="data[pid]">
                                <option value="0" >请选择菜单</option>
                                {foreach $list as $vo}
                                <option value="{$vo.id}" >{$vo.name}</option>
                                {/foreach}

                            </select>

                        </div>
                    </div>

                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">菜单名称：</label>
                        <div class="input-group col-sm-4">
                            <input  id="menuname" type="text" class="form-control" name="data[name]" value="">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">菜单类型：</label>
                        <div class="input-group col-sm-4">
                            <select id="menutype" class="form-control m-b chosen-select" name="data[type]">
                                <option value="view" >链接</option>
                                <option value="click" >点击</option>
                            </select>

                        </div>
                    </div>



                    <div class="hr-line-dashed"></div>
                    <div id="menutype1" class="form-group " style="display: none;">
                        <label class="col-sm-3 control-label">菜单关键词：</label>
                        <div class="input-group col-sm-4">
                            <input id="menukey" type="text" class="form-control" name="data[key]" value=" ">
                        </div>
                    </div>
                    <div id="menutype0" class="form-group">
                        <label class="col-sm-3 control-label">URl连接地址：</label>
                        <div class="input-group col-sm-4">
                            <input id="menuurl"  type="text" class="form-control" name="data[url]" value=" ">
                        </div>
                    </div>


                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">状&nbsp;&nbsp;态：</label>
                        <div class="col-sm-6">
                            <div class="radio ">
                                <input id="status" type="checkbox" name='data[status]' value="1" class="js-switch" checked />&nbsp;&nbsp;默认开启
                            </div>
                        </div>
                    </div>

                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">排&nbsp;&nbsp;序：</label>
                        <div class="input-group col-sm-4">
                            <input id="menusort" type="text" class="form-control" name="data[sort]" value="25">
                        </div>
                    </div>

                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-3">
                            <button id="sub" class="btn btn-primary"  ><i class="fa fa-save"></i> 保存</button>&nbsp;&nbsp;&nbsp;
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>菜单设置</h5>
        </div>
        <div class="ibox-content">
            <!--搜索框开始-->           
            <div class="row">
                <div class="col-sm-12">   
                <div  class="col-sm-2" style="width: 100px">
                    <div class="input-group" >  
                        <button class="btn btn-outline btn-primary" onclick="modaladd()"  type="button">添加菜单</button>

                    </div>
                </div>                                                                    
                </div>
            </div>
            <!--搜索框结束-->
            <div class="hr-line-dashed"></div>

            <div class="example-wrap">
                <div class="example">
                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr class="long-tr">
                                <th>ID</th>
                                <th>菜单名称</th>
                                <th>关键词</th>
                                <th>外连URL</th>
                                <th >状态</th>
                                <th >操作</th>
                            </tr>
                        </thead>
                        <tbody id="article_list">
                        {foreach $list as $vo}
                        <tr class="long-td">
                            <td>{$vo.id}</td>
                            <td style="text-align: left;">{$vo.name}</td>
                            <td>{$vo.key}</td>
                            <td>{$vo.url}</td>

                            <td>
                                {eq name="$vo.status" value="1"}
                                <a href="javascript:;" >
                                    <div  ><span class="label label-info">开启</span></div>
                                </a>
                                {else/}
                                <a href="javascript:;" >
                                    <div  ><span class="label label-danger">禁用</span></div>
                                </a>
                                {/eq}

                            </td>
                            <td>
                                <a onclick="modaledit({$vo.id})"   class="btn btn-primary btn-xs">
                                    <i class="fa fa-paste"></i> 编辑</a>&nbsp;&nbsp;
                                <a href="javascript:;" onclick="del_cate({$vo.id})" class="btn btn-danger btn-xs">
                                    <i class="fa fa-trash-o"></i> 删除</a>
                            </td>
                        </tr>
                            {foreach $vo['sub_button'] as $vovo}
                        <tr class="long-td">
                            <td>{$vovo.id}</td>
                            <td style="text-align: left;">|---- {$vovo.name}</td>
                            <td>{$vovo.key}</td>
                            <td>{$vovo.url}</td>

                            <td>
                                {eq name="$vovo.status" value="1"}
                                <a href="javascript:;" >
                                    <div  ><span class="label label-info">开启</span></div>
                                </a>
                                {else/}
                                <a href="javascript:;" >
                                    <div  ><span class="label label-danger">禁用</span></div>
                                </a>
                                {/eq}
                            </td>
                            <td>
                                <a onclick="modaledit({$vovo.id})"   class="btn btn-primary btn-xs">
                                    <i class="fa fa-paste"></i> 编辑</a>&nbsp;&nbsp;
                                <a href="javascript:;" onclick="del_cate({$vovo.id})" class="btn btn-danger btn-xs">
                                    <i class="fa fa-trash-o"></i> 删除</a>
                            </td>
                        </tr>
                            {/foreach}
                        {/foreach}


                        </tbody>
                        <tfoot>
                        <tr class="long-td">
                            <td colspan="6"><div class="col-sm-4 col-sm-offset-3">
                                <button id="shengcheng" class="btn btn-primary" type="submit"> 生  成</button>
                            </div></td>
                        </tr>

                        </tfoot>
                    </table>
                </div>
            </div>

        </div>
    </div>
</div>

<!-- End Panel Other -->
</div>
{include file="public/footer" /}


<script type="text/javascript">
    //提交
    $(function(){
        $('#add_menu').ajaxForm({
            beforeSubmit: checkForm,
            success: complete,
            dataType: 'json'
        });

        function checkForm(){
            if( '' == $.trim($('#menuname').val())){
                layer.msg('请输入菜单名称',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

            if($('#pid').val() !=0 &&( '' == $.trim($('#menukey').val()) && '' == $.trim($('#menuurl').val()))){
                layer.msg('请输入关键词或URL',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

        }


        function complete(data){
            if(data.code==1){
                layer.msg(data.msg, {icon: 6,time:1500,shade: 0.1}, function(index){
                  //  window.location.href="{:url('config/index')}";
                    window.location.reload(true);
                });
            }else{
                layer.msg(data.msg, {icon: 5,time:1500,shade: 0.1});
                return false;
            }
        }
        $('#menutype').change(function () {
            if($(this).val()=='view'){
                $("#menutype0").show();
                $('#menukey').val('');
                $("#menutype1").hide();
            }else{
                $("#menutype1").show();
                $('#menuurl').val('');
                $("#menutype0").hide();
            }
        });
        
        //生成
        $('#shengcheng').click(function () {
            $.getJSON('{:URL("menugenerate")}', function (res) {
                if(res.code == 1){
                    layer.msg(res.msg,{icon:1,time:1500,shade: 0.1});
                }else{
                    layer.msg(res.msg,{icon:0,time:1500,shade: 0.1});
                }
            })
        });

    });







    //IOS开关样式配置
    var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });


    function setSwitchery(switchElement, checkedBool) {
        if((checkedBool && !switchElement.isChecked()) || (!checkedBool && switchElement.isChecked())) {
            switchElement.setPosition(true);
            switchElement.handleOnchange(true);
        }
    }








    /**
 * [del 删除用户]
 * @Author[田建龙 864491238@qq.com]
 * @param   {[type]}    id [用户id]
 */
function del_cate(id){
    layer.confirm('确认删除此菜单?', {icon: 3, title:'提示'}, function(index){
        //do something
        $.getJSON('./menudel', {'id' : id}, function(res){
            if(res.code == 1){
                layer.msg(res.msg,{icon:1,time:1500,shade: 0.1},function(){
                    window.location.href="{:url('menu')}";
                });               
            }else{
                layer.msg(res.msg,{icon:0,time:1500,shade: 0.1});
            }
        });

        layer.close(index);
    })

}

    function modaladd(){
        $('#add_menu').attr('action',"{:URL('menuadd')}");
        $('#id').remove();
        $("#modal-edit").modal("show");
    }
    function modaledit(id){
        $('#add_menu').attr('action',"{:URL('menuedit')}");
        $('#id').remove();
        $.get("{:URL('menuedit')}",{id:id}, function (res) {
            if(res.code == 1){

                var data=res.data;
                $("#pid").val(data.pid);
                $('#menuname').val(data.name);
                $('#add_menu').append('<input type="hidden" name="id" id="id" value="'+data.id+'">');
                if(data.status==0){
                    setSwitchery(switchery,false);
                }else{
                    setSwitchery(switchery,true);
                }

                $('#menuurl').val(data.url);
                $('#menukey').val(data.key);

                $('#menusort').val(data.sort);
            }
        });
        $("#modal-edit").modal("show");
    }


</script>
</body>
</html>